/*=========================================================================

Program:   Virtual Surgery
Language:  C++
Date:      2009-9-1
Version:   0.1
Mail:		   nelfylee@gmail.com

Copyright (c) 2009 Nelfy Lee. All rights reserved.

=========================================================================*/
#include "CenterLinePanel.h"

#include "ControlPanelDock.h"
#include "SegmentationPanels.h"
#include "SmoothingPanel.h"
#include "ControlPanelBase.h"
#include "PipelineModel.h"
#include "SurfacePanel.h"
#include "VolumePanel.h"
#include "ThresholdingPanel.h"

#include <QDebug>
#include <QScrollArea>


ControlPanelDock::ControlPanelDock(QWidget *parent) : QDockWidget( "Control Panel", parent)
{
	setMinimumHeight(380);

	m_currentSource = NULL;

	QFrame *frame = new QFrame(this);
	setWidget(frame);
	
	QHBoxLayout *hlayout = new QHBoxLayout();

	QLabel *label = new QLabel(QString(tr(" Current: ")), frame);
	QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
	sizePolicy.setHorizontalStretch(0);
	sizePolicy.setVerticalStretch(0);
	sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
	label->setSizePolicy(sizePolicy);
	hlayout->addWidget(label);

	m_sourceName = new QLabel("None",frame);
	QFont font;
	font.setBold(true);
	font.setWeight(75);
	m_sourceName->setFont(font);
	m_sourceName->setStyleSheet(QString::fromUtf8("color: rgb(170, 0, 0);"));
	m_sourceName->setFixedWidth(150);

	hlayout->addWidget(m_sourceName);

	QToolButton *btn_home = new QToolButton(frame);
	btn_home->setIcon(QIcon(QPixmap(":/res/home.png")));
	btn_home->setIconSize(QSize(16, 16));
	btn_home->setAutoRaise(true);
	btn_home->setCursor(QCursor(Qt::PointingHandCursor));
	connect(btn_home, SIGNAL(clicked()), SLOT(slotShowHome()));
	hlayout->addWidget(btn_home);
	hlayout->setMargin(5);

	QVBoxLayout *vlayout = new QVBoxLayout(frame);
	vlayout->setSpacing(0);
	vlayout->setMargin(0);
	vlayout->addLayout(hlayout);
	
	QFrame *line = new QFrame(this);
	line->setFrameShape(QFrame::HLine);
	line->setFrameShadow(QFrame::Sunken);
	vlayout->addWidget(line);


	m_scrollArea = new QScrollArea(frame);
	m_scrollArea->setWidgetResizable(true);
	m_scrollArea->setFrameShape(QFrame::NoFrame);
	vlayout->addWidget(m_scrollArea);

	//slotShowPanel(ptHome);

	m_panelHome = new ControlPanelHome(this);
	m_panels.push_back(m_panelHome);
	connect(m_panelHome, SIGNAL(emitShowPanel(PanelType)), SLOT(slotShowPanel(PanelType)));
	

	m_scrollArea->setWidget(m_panelHome);

// 	m_comboxPanels->addItem(QString("Main"));
// 	m_comboxPanels->addItem(QString("Smoothing"));
// 	m_comboxPanels->addItem(QString("Vessel"));
// 	m_comboxPanels->addItem(QString("Liver"));
// 	m_comboxPanels->addItem(QString("Test"));

	//connect(m_comboxPanels, SIGNAL(currentIndexChanged(int)), SLOT(slotShowPanel(int)));
}


ControlPanelDock::~ControlPanelDock(){}


void 
ControlPanelDock::setDisplayModel(DisplayModel *dm)
{
	m_displayModel = dm;

	m_panelHome->setDisplayModel(m_displayModel);

	// set it to sub panels
}

void 
ControlPanelDock::setPipeline(PipelineModel *pipeline)
{
	m_pipelineModel = pipeline;

	connect(m_pipelineModel, SIGNAL(emitSourceSelected(const QModelIndex&)), SLOT(slotSourceSelected(const QModelIndex&)));
	connect(m_pipelineModel, SIGNAL(emitSourcePreRemove(const QModelIndex&)), SLOT(slotSourcePreRemove(const QModelIndex&)));
	m_panelHome->setPipelineModel(m_pipelineModel);
}


void
ControlPanelDock::slotSourceSelected(const QModelIndex& index)
{
	if ( !index.isValid())
		return;

	m_isource = index;

	m_currentSource = (Source*)m_pipelineModel->itemFromIndex(index);

	QWidget *w = m_scrollArea->widget();
	ControlPanelBase *panel = static_cast<ControlPanelBase*>(w);

	m_sourceName->setText(m_currentSource->text());

	if (panel)
		panel->setSource(m_isource);
}

void 
ControlPanelDock::slotSourcePreRemove(const QModelIndex& index)
{
	slotShowPanel(ptHome);
	m_currentSource = NULL;
}


ControlPanelBase*
ControlPanelDock::createPanel(PanelType pt)
{
	if (!m_currentSource)
	{
		m_panelHome->setPipelineModel(m_pipelineModel);
		m_panelHome->setDisplayModel(m_displayModel);
		m_panelHome->setSource(m_isource);
		return m_panelHome;
	}

	ControlPanelBase *panel = findPanel(pt);

	if ( panel != NULL)
		return panel;

	switch(pt)
	{
	case ptSmoothing:
			panel = new SmoothingPanel(this);
			break;
	case ptVesselSegmentation:
			panel = new VesselProcessingPanel(this);
			break;
	case ptLiverSegmentation:
			panel = new LiverProcessingPanel(this);
			break;
	case ptSurface:
			panel = new SurfacePanel(this);
			break;
	case ptVolume:
			panel = new VolumePanel(this);
			break;
	case ptThresholding:
			panel = new ThresholdingPanel(this);
			break;
	case ptCenterLine:
			panel = new CenterLinePanel(this);
			break;
	case ptHome:
	default:
			panel = m_panelHome;
			break;
	}

	panel->setPipelineModel(m_pipelineModel);
	panel->setDisplayModel(m_displayModel);
	panel->setSource(m_isource);
	m_panels.push_back(panel);

	return panel;
}


ControlPanelBase*
ControlPanelDock::slotShowHome()
{
	slotShowPanel(ptHome);
	return NULL;
}


ControlPanelBase* 
ControlPanelDock::slotShowPanel(PanelType pt)
{
	QWidget *w = m_scrollArea->widget();
	ControlPanelBase *panel = static_cast<ControlPanelBase*>(w);

// 	if (panel && panel->panelType()==ptHome)
// 	{
// 		m_panelHome = (ControlPanelHome*)m_scrollArea->takeWidget();
// 	}

	if ( panel)
	{
		ControlPanelBase *temp = findPanel(pt);
		temp = (ControlPanelHome*)m_scrollArea->takeWidget();
	}

	m_scrollArea->setWidget(createPanel(pt));

	return NULL;
}

ControlPanelBase* ControlPanelDock::findPanel( PanelType pt )
{
	foreach(ControlPanelBase *cpb, m_panels)
	{
		if ( cpb->panelType() == pt)
		{
			return cpb;
		}
	}

	return NULL;
}
